package com.yuyaogc.lowcode.engine.util;

import okhttp3.*;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class OkHttpClient {
    private static final okhttp3.OkHttpClient client = new okhttp3.OkHttpClient().newBuilder().connectTimeout(600, TimeUnit.SECONDS)
            .readTimeout(600, TimeUnit.SECONDS).writeTimeout(600, TimeUnit.SECONDS).build();

    public static CompletableFuture<MetaHttpResponse> postAsync(String url, Map<String, String> headersMap, String body) {

        Request request = createRequest(url, headersMap, body);
        CompletableFuture<MetaHttpResponse> completable = new CompletableFuture<MetaHttpResponse>();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                completable.completeExceptionally(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                completable.complete(new MetaHttpResponse(response));
            }
        });
        return completable;
    }

    public static class MetaHttpResponse {
        private final Response response;

        public MetaHttpResponse(Response response) {
            this.response = response;
        }

        public boolean isSuccessful() {
            return response.isSuccessful();
        }

        public int code() {
            return response.code();
        }

        public ResponseBody body() {
            return response.body();
        }

        public void close() {
            response.close();
        }
    }

    private static Request createRequest(String url, Map<String, String> headersMap, String body) {
        Request.Builder builder = new Request.Builder().url(url);
        if (headersMap != null && headersMap.size() > 0) {
            builder = builder.headers(Headers.of(headersMap));
        }
        Request request = builder.post(RequestBody.create(MediaType.parse("application/json"), body)).build();
        return request;
    }

    private static Request createRequest(String url, Map<String, String> headersMap, byte[] body) {

        Request.Builder builder = new Request.Builder().url(url);
        if (headersMap != null && headersMap.size() > 0) {
            builder = builder.headers(Headers.of(headersMap));
        }
        Request request = builder.post(RequestBody.create(MediaType.parse("application/json"), body)).build();
        return request;
    }

    public static Response post(String url, Map<String, String> headersMap, String body) throws IOException {
        Request request = createRequest(url, headersMap, body);
        return client.newCall(request).execute();
    }

    public static Response post(String url, Map<String, String> headersMap, byte[] body) throws IOException {
        Request request = createRequest(url, headersMap, body);
        return client.newCall(request).execute();
    }


    public static Response upload(String url, Map<String, String> headersMap, RequestBody requestBody) throws IOException {

        Request.Builder builder = new Request.Builder().url(url);
        if (headersMap != null && headersMap.size() > 0) {
            builder = builder.headers(Headers.of(headersMap));
        }

        Request request = builder.post(requestBody).build();
        return client.newCall(request).execute();
    }
}